/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License Version
 *  3 as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
 *  USA.
 */

#include "menubutton.h"

//#include <QxtToolTip>
#include <QAction>

O_PRIVATE_DECLARE(MenuButton){

public:
    virtual ~MenuButtonPrivate() {};

    MenuButtonPrivate():action(0)
    {

    }

    QPixmap pixmap;
    QString data;
    QAction* action;
    QRectF rect;
};


MenuButton::MenuButton(const QPixmap &pixmap, QGraphicsItem *parent)
    : QGraphicsWidget(parent), O_PRIVATE_CREATE(MenuButton)
{
    O_D(MenuButton)

    setAcceptHoverEvents(true);
    setCacheMode(DeviceCoordinateCache);

    d->pixmap = pixmap;
    d->rect = QRectF(-100, -100, 200, 200);
}

MenuButton::MenuButton(QAction* action, const QRectF& rect,  QGraphicsItem *parent)
    :QGraphicsWidget(parent), O_PRIVATE_CREATE(MenuButton)
{
    O_D(MenuButton)

    setAcceptHoverEvents(true);
    setCacheMode(DeviceCoordinateCache);
    d->rect = rect;
    d->action = action;
    d->pixmap = action->icon().pixmap(70, 70);
}

MenuButton::~MenuButton()
{
   O_PRIVATE_DELETE;
}

QRectF MenuButton::boundingRect() const
{
    O_D(const MenuButton);
    return d->rect;
}

void MenuButton::setData(const QString& data)
{
    O_D(MenuButton)
    d->data = data;
}

QPainterPath MenuButton::shape() const
{
    QPainterPath path;
    path.addEllipse(boundingRect());
    return path;
}

void MenuButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
    O_D(MenuButton)
    bool down = option->state & QStyle::State_Sunken;
    QRectF r = boundingRect();
    QLinearGradient grad(r.topLeft(), r.bottomRight());

    QColor col1(Qt::black);
    col1.setAlphaF(0.8);

    QColor colw(Qt::white);
    colw.setAlphaF(0.9);

    grad.setColorAt(down ? 1 : 0, option->state & QStyle::State_MouseOver ? colw : col1);

    col1.setAlphaF(0.1);

    grad.setColorAt(down ? 0 : 1, col1);
    painter->setPen(Qt::yellow);
    painter->setBrush(grad);
    painter->drawEllipse(r);

    QLinearGradient grad2(r.topLeft(), r.bottomRight());
    grad.setColorAt(down ? 1 : 0, col1);
    grad.setColorAt(down ? 0 : 1, colw);
    painter->setPen(Qt::NoPen);
    painter->setBrush(grad);
    if (down)
        painter->translate(2, 2);
    painter->drawEllipse(r.adjusted(5, 5, -5, -5));
    if (!d->pixmap.isNull())
        painter->drawPixmap(-d->pixmap.width()/2, -d->pixmap.height()/2, d->pixmap);
    else
        painter->drawText(r, "<empty>", Qt::AlignHCenter | Qt::AlignVCenter);
}

void MenuButton::mousePressEvent(QGraphicsSceneMouseEvent *)
{
    O_D(MenuButton)
    emit pressed();
    emit pressed(d->data);
    if (d->action)
        d->action->trigger();
    update();
}

void MenuButton::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
{
    update();
}


void MenuButton::hoverMoveEvent (QGraphicsSceneHoverEvent * event)
{
    O_D(MenuButton)
    emit hover(d->data);
    update();
}

void MenuButton::hoverLeaveEvent (QGraphicsSceneHoverEvent * event)
{
    O_D(MenuButton)
    emit leave(d->data);
    update();
}
